package com.microservice.product.controller;

import com.microservice.common.R;
import com.microservice.product.dto.ProductInfoDTO;
import com.microservice.product.entity.ProductEntity;
import com.microservice.product.feign.ProductClient;
import com.microservice.product.param.ProductReduceParam;
import com.microservice.product.service.ProductService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * write something please~
 *
 * @author huangrz
 * @since 2021/9/14
 **/
@RestController
@RequestMapping("/product")
public class ProductController implements ProductClient {

    @Autowired
    private ProductService productService;
    @Autowired
    private ModelMapper modelMapper;

    @PostMapping("/reduce")
    @Override
    public R<Boolean> reduce(@RequestBody ProductReduceParam param) {
        return productService.reduce(param);
    }

    @GetMapping("/get/{id}")
    @Override
    public R<ProductInfoDTO> getById(@PathVariable Long id) {
        ProductEntity productEntity = productService.getById(id);
        if (productEntity == null) {
            return R.ok(null);
        }
        return R.ok(modelMapper.map(productEntity, ProductInfoDTO.class));
    }

}
